package org.ceva.training.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.ceva.training.persistence.bo.Course;
import org.ceva.training.persistence.bo.CourseDocument;
import org.ceva.training.persistence.bo.User;
import org.ceva.training.persistence.dao.CourseDAO;
import org.ceva.training.persistence.dao.UsersDAO;

public class CourseModel {
	@Resource
	protected CourseDAO courseDAO;
	
	protected Course selected;
	protected CourseDocument docselected;
	protected String queryString;
	protected String where;
	protected String orderBy;
	protected int offset;
	protected int maxResults;
	protected Map<String, ?> parameters;
	
	public CourseDAO getDAO() {
		return courseDAO;
	}
	
	public void setDAO(CourseDAO todoDAO) {
		this.courseDAO = todoDAO;
	}
	
	public Course getSelected() {
		return this.selected;
	}
	
	public void setSelected(Course todo) {
		this.selected = todo;
	}
	
	public void setWhere(String where) {
		this.where = where;
	}
	
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	
	public void setOffset(int offset) {
		this.offset = offset;
	}
	
	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}
	
	public String getQueryString() {
		if (queryString != null) {
			return this.queryString;
		}
		return courseDAO.generateQueryString(this.where, this.orderBy);
	}
	
	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}
	
	public Map<String,?> getParameters() {
		return this.parameters;
	}
	
	public void setParameters(Map<String,?> params) {
		this.parameters = params;
	}
	
	//-- DB access on the selected bean --//
	public void persist() {
		courseDAO.save(selected);
	}
	
	public void merge(){
		courseDAO.merge(selected);
	}
	
	public void delete(){
		courseDAO.delete(selected);
	}
	
	public List<Course> getAll() {
		String query = courseDAO.generateQueryString("","");
		return courseDAO.find(query, this.offset, this.maxResults);
	}

	public CourseDocument getDocselected() {
		return docselected;
	}

	public void setDocselected(CourseDocument docselected) {
		this.docselected = docselected;
	}
	
	
	
}
